package com.wsc.wsckeeper.adapter;

import java.text.SimpleDateFormat;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wsc.wsckeeper.R;
import com.wsc.wsckeeper.managers.ResourceManager;
import com.wsc.wsckeeper.model.entity.AccountE;

@SuppressLint("SimpleDateFormat")
public class AccountsAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private List<AccountE> accounts;
	private SimpleDateFormat sdf;

	public AccountsAdapter(Context context, List<AccountE> accounts) {
		this.accounts = accounts;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		sdf = new SimpleDateFormat(ResourceManager.getStringValue(R.string.format_history_date));
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;

		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.adapter_item_account, null);
			holder.accountDestination = (TextView) convertView.findViewById(R.id.item_account_destination);
			holder.accountLogin = (TextView) convertView.findViewById(R.id.item_account_login);
			holder.created = (TextView) convertView.findViewById(R.id.item_created);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		AccountE item = accounts.get(position);
		if (item != null) {
			holder.accountDestination.setText(item.getDescription());
			holder.accountLogin.setText(item.getLogin());
			holder.created.setText(sdf.format(item.getCreated()));
		}
		return convertView;
	}

	class ViewHolder {
		TextView accountDestination;
		TextView accountLogin;
		TextView created;
	}

	@Override
	public int getCount() {
		return accounts.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
}